#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <erpc_client_setup.h>
#include <erpc_port.h>
#include "hello.h"

static void free_binary_t_struct(binary_t *data)
{
    if (data->data) {
        erpc_free(data->data);
    }
}

int main(int argc, char *argv[])
{
    const char *server = "192.168.2.56";

    if (argc == 2) {
        server = argv[1];
    }
    erpc_transport_t transport = erpc_transport_tcp_init(server, 5555, false);
    erpc_mbf_t message_buffer_factory = erpc_mbf_dynamic_init();
    erpc_client_init(transport, message_buffer_factory);
    char *msg = "hello erpc!";
    binary_t b = {(uint8_t *)msg, (uint32_t)strlen(msg)};
    binary_t *resp = sayHello(&b);
    if (resp != NULL) {
        char *buf = (char *)erpc_malloc(resp->dataLength + 1);
        strncpy(buf, (const char *)resp->data, resp->dataLength);
        buf[resp->dataLength] = '\0';
        printf("respond: %s\n", buf);
        free_binary_t_struct(resp);
        free(resp);
        free(buf);
    }

    erpc_transport_tcp_close();

    return 0;
}
